package cn.birdbaby.plugins.selector.field;

import cn.birdbaby.plugins.selector.PsiElementSelector;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author llorando 9/8/24 1:15 pm
 * 完成字段粒度的筛选
 */
public abstract class PsiFieldSelector extends PsiElementSelector {

    protected Map<PsiJavaFile, List<PsiElement>> matchInFile(PsiFile javaFile) {
        if(!match(javaFile)) return defaultReturn();
        Collection<PsiClass> childrenOfAnyType = PsiTreeUtil.findChildrenOfAnyType(javaFile, PsiClass.class);

        if (CollectionUtils.isEmpty(childrenOfAnyType)) return defaultReturn();

        Map map=new HashMap();
        map.put(javaFile,childrenOfAnyType.stream().flatMap(psiClass -> Arrays.stream(psiClass.getFields())).collect(Collectors.toList()));
        return map;

    }
}
